home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / mslang / poetmf / src / persdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-23  |  16.3 KB  |  565 lines

  1. // ******************************************************************
  2. //
  3. //  persdlg.cpp : implementation file
  4. //
  5. //    Implemented    classes:
  6. //        CEditPersDlg
  7. // 
  8. //  Author: Nils Behncke
  9. //            POET Software GmbH, August 1993
  10. //
  11. // ******************************************************************
  12.  
  13.  
  14. #include <stdafx.h>     // header to MFC
  15. #include <afxdlgs.h>    // header to standarddialogs of MFC
  16. #include <persdlg.h>    // header to this file
  17. #include <adrdlg.h>        // header to addressdialog
  18. #include <stepdlg.h>    // header to stepdialog
  19. #include <pictdlg.h>    // header to picturedialog
  20. #include <helloapp.h>    // header to application and window-class
  21.  
  22.  
  23. extern CHelloWinApp NEAR theApp;
  24.  
  25. //    class CEditPersDlg derived from CDialog and created by ClassWizard
  26.  
  27. #ifdef _DEBUG        //    added by ClassWizard
  28. #undef THIS_FILE
  29. static char BASED_CODE THIS_FILE[] = __FILE__;
  30. #endif
  31.  
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CEditPersDlg dialog
  35.  
  36. CEditPersDlg::CEditPersDlg(CWnd* pParent, UINT nIDTemp )    //    extended constructor (see comment in headerfile)
  37.     : CDialog(nIDTemp, pParent)
  38. {
  39.     //{{AFX_DATA_INIT(CEditPersDlg)
  40.         //    member variables will be initialized in member funtion Init
  41.     //}}AFX_DATA_INIT
  42. }       
  43.  
  44.  
  45. CEditPersDlg::~CEditPersDlg ()
  46. {
  47.     if (allperson)
  48.         delete allperson;
  49.     if (person)
  50.         person->Forget ();  
  51. }
  52.  
  53. void CEditPersDlg::POET_Error (char *text, int error)
  54. {
  55.     char     buffer[100];
  56.     char    title[100];
  57.         
  58.     PtError (buffer, text, error);            //    build errortext
  59.     GetWindowText (title, sizeof (title));  //    get title of current window
  60.     MessageBox (buffer, title, MB_OK);        //    show errormessage
  61.  
  62.  
  63. void CEditPersDlg::DoDataExchange(CDataExchange* pDX)
  64. {
  65.     CDialog::DoDataExchange(pDX);
  66.     //{{AFX_DATA_MAP(CEditPersDlg)
  67.     DDX_Control(pDX, IDC_LIST_FIRSTNAMES, m_CListFirstnames);
  68.     DDX_Control(pDX, IDC_LIST_ADDRESSES, m_CListAddresses);
  69.     DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
  70.     DDX_Text(pDX, IDC_EDIT_FIRSTNAME, m_strFirstname);
  71.     //}}AFX_DATA_MAP
  72. }
  73.  
  74. void CEditPersDlg::ClearControls ()
  75. {
  76.     m_CListFirstnames.ResetContent ();
  77.     m_CListAddresses.ResetContent ();
  78.     m_strName = "";
  79.     m_strFirstname = "";
  80. }
  81.  
  82. BEGIN_MESSAGE_MAP(CEditPersDlg, CDialog)
  83.     //{{AFX_MSG_MAP(CEditPersDlg)
  84.     ON_BN_CLICKED(IDC_ADR_UPDATE, OnClickedAdrUpdate)
  85.     ON_BN_CLICKED(IDC_ADR_NEW, OnClickedAdrNew)
  86.     ON_BN_CLICKED(IDC_STEP, OnClickedStep)
  87.     ON_BN_CLICKED(IDC_STORE, OnClickedStore)
  88.     ON_BN_CLICKED(IDC_NEXT, OnClickedNext)
  89.     ON_BN_CLICKED(IDC_COPY, OnClickedCopy)
  90.     ON_BN_CLICKED(IDC_DELETE, OnClickedDelete)
  91.     ON_BN_CLICKED(IDC_PREV, OnClickedPrev)
  92.     ON_BN_CLICKED(IDC_ASSPIC, OnClickedAsspic)
  93.     ON_EN_CHANGE(IDC_EDIT_FIRSTNAME, OnChangeEditFirstname)
  94.     ON_EN_KILLFOCUS(IDC_EDIT_FIRSTNAME, OnKillfocusEditFirstname)
  95.     ON_BN_CLICKED(IDC_ADR_DELETE, OnClickedAdrDelete)
  96.     ON_BN_CLICKED(IDC_CLEAR, OnClickedClear)
  97.     ON_LBN_DBLCLK(IDC_LIST_ADDRESSES, OnDblclkListAddresses)
  98.     ON_LBN_DBLCLK(IDC_LIST_FIRSTNAMES, OnDblclkListFirstnames)
  99.     ON_BN_CLICKED(IDC_SHOWPIC, OnClickedShowpic)
  100.     //}}AFX_MSG_MAP
  101. END_MESSAGE_MAP()
  102.  
  103.  
  104.  
  105. void CEditPersDlg::Show ()
  106. {
  107.     PtString        fname;
  108.     Address            *adr;
  109.     int register     err = 0;
  110.     char            buffer[100];                    
  111.     
  112.     ClearControls ();    // initialize Controls
  113.     
  114.     m_strName = person->GetName ();            // set name
  115.     
  116.     if (person->firstnames.Seek (0L, PtSTART) == 0 && person->firstnames.Get (fname) == 0)
  117.         m_strFirstname = CString (fname);    // set firstname    
  118.         
  119.     for (                                     // set firstnames
  120.          err=person->firstnames.Seek (0L, PtSTART);
  121.          (err==0) && (person->firstnames.Get (fname) == 0);
  122.          err = person->firstnames.Seek (1L, PtCURRENT)
  123.         )
  124.         m_CListFirstnames.AddString (CString(fname));  
  125.     
  126.     for (                                    // set addresses
  127.          err=person->adrs.Seek (0L, PtSTART);
  128.          (err==0) && person->adrs.Get (adr) == 0;
  129.          person->adrs.Unget (adr),
  130.          err = person->adrs.Seek (1L, PtCURRENT)
  131.         )
  132.         m_CListAddresses.AddString (adr->Print (buffer)); 
  133.     UpdateData (FALSE);                     //    set values to dialog membervariables
  134. }    
  135.  
  136.  
  137.  
  138.  
  139. void CEditPersDlg::Init ()
  140. {
  141.     int        err = 0;
  142.     
  143.     firstname_is_modified = 0;        //    editbox firstnames not changed
  144.     NewAllSetPtr ();    //    create    polymorph AllSet
  145.                             
  146.     if ((err=allperson->Seek (0L, PtSTART)) == 0)    //    looking for first entry 
  147.         err = allperson->Get (person);
  148.     if (err != 0)                                   //    if no person in allset
  149.        {
  150.         NewPersonPtr ();    //    allocate memory for polymorph person
  151.         ClearControls ();
  152.         person->Init ();
  153.         person->Assign (theApp.GetOA ());
  154.        }
  155.     Show ();             //    show person in dialog
  156. }
  157.  
  158. void CEditPersDlg::Fill ()
  159. {
  160.     UpdateData ();
  161.     person->SetName (m_strName.GetBuffer (m_strName.GetLength ()));    // actualize name
  162.     m_strName.ReleaseBuffer ();
  163.  
  164.     int index;
  165.     char buffer[100];                                
  166.                                                 
  167.     person->firstnames.Clear ();
  168.     for (index = 0; index < m_CListFirstnames.GetCount (); )        //    actualize firstnames
  169.        {
  170.         m_CListFirstnames.GetText (index++, buffer);
  171.         PtString fname;
  172.         fname = buffer;
  173.         person->firstnames.Append (fname);
  174.        }     
  175. }                                                                    //    addresses already updated
  176.  
  177. static char filebuffer[10240];
  178.  
  179. void CEditPersDlg::LoadBitmap ( char * Name, PtBlob * pic )
  180. {
  181.     HFILE file = _lopen((LPSTR) Name, OF_READ);        //    try to read file
  182.     if (file == -1)
  183.        {
  184.            MessageBox("filename does not exist", "file name alert", MB_OK);
  185.         return;
  186.        }
  187.     UINT Len = 0;
  188.     while ( Len = _lread ( file, filebuffer, sizeof(filebuffer)))    //    write buffer into Blob
  189.         pic->Append ( filebuffer, Len );
  190.  
  191.     _lclose(file);
  192.     return;
  193. }
  194.  
  195. void CEditPersDlg::NewPersonPtr ()
  196. {
  197.     person = new Person ();
  198. }
  199.  
  200. void CEditPersDlg::NewAllSetPtr ()
  201. {
  202.     allperson = new PersonAllSet (theApp.GetOA ());    //    create    PersonAllSet
  203. }
  204.     
  205. /////////////////////////////////////////////////////////////////////////////
  206. // CEditPersDlg message handlers
  207.  
  208.  
  209. BOOL CEditPersDlg::OnInitDialog()
  210. {   
  211.     CDialog::OnInitDialog();    
  212.     
  213.     Init ();    //    initialize controls befor show dialogwindow
  214.  
  215.     return TRUE;  // return TRUE  unless you set the focus to a control
  216. }
  217.  
  218.  
  219. void CEditPersDlg::OnClickedAdrUpdate()
  220. {   
  221.             
  222.     if ((item = m_CListAddresses.GetCurSel ()) >= 0)
  223.      {
  224.       Address *a;    //    new address
  225.       int nResult;
  226.  
  227.       person->adrs.Seek ( (long) item, PtSTART);    //    set position in set on current selection in listbox
  228.       person->adrs.Get (a);        //    fill new address
  229.  
  230.       CAddressDlg updateaddress (this);                //    create dialog
  231.       updateaddress.SetAddress (a->GetStreet (), a->GetZip (), a->GetCity ());    // initialize dialog
  232.       nResult = updateaddress.DoModal ();        //    start dialog    
  233.  
  234.       if (nResult == IDOK)    // update address only on clicked OK button
  235.        {
  236.         char buffer [100];    
  237.  
  238.         a->SetAddress ( updateaddress.GetStreet (),    //    update new address a
  239.                           updateaddress.GetZip (),
  240.                         updateaddress.GetCity () );
  241.         m_CListAddresses.DeleteString (item);    //    delete old address in listbox
  242.         m_CListAddresses.InsertString (item, a->Print (buffer));        // insert address-string in listbox
  243.         a->Assign (theApp.GetOA ());    //    assign new address to database 
  244.         person->adrs.Put (a);    //    replace old with new address in the set of addresses
  245.        }
  246.       person->adrs.Unget (a);    //    free resources allocated by Get
  247.       updateaddress.EndDialog (nResult);            //    end dialog
  248.      } 
  249. }
  250.  
  251.  
  252. void CEditPersDlg::OnClickedAdrNew()
  253. {   
  254.     Address *a = new Address ();        //    new address        
  255.  
  256.     CAddressDlg newaddress (this);        //    create dialog
  257.     newaddress.SetAddress ("", 0, "");    //    initialize dialog
  258.     int nResult = newaddress.DoModal ();        //    call dialog    
  259.  
  260.     if (nResult == IDOK)    //    add new address only on OK
  261.      {
  262.       char buffer [100];    
  263.  
  264.       a->SetAddress ( newaddress.GetStreet (),    //    update new address a
  265.                       newaddress.GetZip (),
  266.                       newaddress.GetCity () );
  267.       m_CListAddresses.AddString (a->Print (buffer));        //    add address-string to listbox
  268.       a->Assign (theApp.GetOA ());    //    assign new address to database 
  269.       person->adrs.Append (a);        //    append new address to set of addresses
  270.       a->Forget ();
  271.      } 
  272.     else
  273.       delete a;
  274.     newaddress.EndDialog (nResult);    //    end dialog
  275.  
  276. }
  277.  
  278. void CEditPersDlg::OnClickedAdrDelete()
  279. {               
  280.     if ((item = m_CListAddresses.GetCurSel ()) >= 0)
  281.      {                       
  282.       person->adrs.Seek ( (long) item, PtSTART);    //    set position to address to delete
  283.       person->adrs.Delete ();                        //    delete address
  284.       m_CListAddresses.DeleteString (item);            //    remove address from listbox
  285.      } 
  286. }
  287.  
  288.  
  289. void CEditPersDlg::OnDblclkListAddresses()
  290. {
  291.     OnClickedAdrUpdate ();        //    update address if doubleclicked
  292. }
  293.  
  294.  
  295. void CEditPersDlg::OnClickedStep()
  296. {                
  297.     int         nResult;
  298.     PtSeekMode     mode;
  299.     long        step;
  300.     int         erc;
  301.  
  302.     CStepDlg stepdlg (this);
  303.     nResult = stepdlg.DoModal ();    //    start dialog
  304.     if (nResult == IDOK)
  305.        {
  306.         step = (long) stepdlg.GetStep ();
  307.         switch (stepdlg.GetSeekmode ())
  308.            {
  309.                case 0 :
  310.                    mode = PtSTART;
  311.                    break;
  312.                case 1 :
  313.                    mode = PtCURRENT;
  314.                    break;
  315.                case 2 :
  316.                    mode = PtEND;
  317.                    break;
  318.            }
  319.         Person    *newperson;   
  320.            
  321.         if ((erc = allperson->Seek (step, mode)) == 0)    //    try to step
  322.             erc = allperson->Get (newperson);
  323.         if (erc == 0)
  324.            {
  325.             person->Forget ();           
  326.             person = newperson;
  327.             ClearControls ();                          
  328.             Show ();                                     //    show actual person
  329.            }
  330.         else       
  331.             POET_Error ("Step", erc);      
  332.        }
  333.     stepdlg.EndDialog (nResult);                        //    end dialog
  334. }
  335.  
  336.  
  337. void CEditPersDlg::OnClickedStore()
  338. {
  339.     int    err;
  340.     Fill ();                                               //    actualize current person
  341.     err = person->Store (PtDEEP);                        //    store person
  342.     POET_Error ("Object is stored", err);                  //    show status
  343. }
  344.  
  345.  
  346. void CEditPersDlg::OnClickedNext()
  347. {
  348.     Person    *newperson;   
  349.     int erc;
  350.               
  351.     if ((erc = allperson->Seek (1L, PtCURRENT)) == 0)    //    try to step 1
  352.         erc = allperson->Get (newperson);
  353.     if (erc == 0)
  354.        {
  355.         person->Forget ();
  356.         person = newperson;
  357.         ClearControls ();
  358.         Show ();                                        //    show actual person
  359.        }
  360.     else
  361.         POET_Error ("Next", erc);                        //    show status
  362. }
  363.  
  364.  
  365. void CEditPersDlg::OnClickedCopy()
  366. {   
  367.     int    erc;                                        
  368.     Address *adr, *newadr;
  369.     
  370.     for (                //    set new references to copied addresses 
  371.          erc=person->adrs.Seek (0L, PtSTART);
  372.          (erc==0) && person->adrs.Get (adr) == 0;
  373.          person->adrs.Unget (adr),
  374.          erc = person->adrs.Seek (1L, PtCURRENT)
  375.         )
  376.        {                                
  377.         char *city = new char[100];
  378.         char *street = new char[100];
  379.         newadr = new Address ();
  380.         newadr->SetAddress ( strcpy (street, adr->GetStreet ()),
  381.                             adr->GetZip (),
  382.                             strcpy (city, adr->GetCity ()) );        
  383.         newadr->Assign (theApp.GetOA ());
  384.         person->adrs.Put (newadr);
  385.         delete city;    
  386.         delete street;
  387.         newadr->Forget ();    //    Remove new address
  388.        }    
  389.  
  390.        Picture pic, *oldpic = NULL;
  391.     PtBlob    *blob = NULL;
  392.     
  393.     if ( person->pict.IsReferenced () )
  394.        {
  395.            int err = person->pict.Get(oldpic);    //    Get old picture
  396.         blob = new PtBlob(oldpic->picture);    //    Copy Blob using copy-constructor
  397.         person->pict.Unget (oldpic);    //    Remove old picture from memory
  398.         pic.SetPicture (*blob);    //    Set new Blob to new picture object
  399.         
  400.         if ( (err = pic.Assign(theApp.GetOA())) != 0)     //    Assign the Picture object to the data base
  401.             POET_Error ("Did not copy picture", err);
  402.         else
  403.             person->pict.SetReference(&pic);    //    Set the reference of the Picture in the
  404.                                                 //    Person object to the new object
  405.        }
  406.  
  407.     person->UnAssign ();                                //    cut connection to database
  408.     Fill ();                                            //    fill independend person with current data
  409.             
  410.     person->Assign (theApp.GetOA ());                   //    assign new person to database 
  411.     erc = person->Store (PtDEEP);                        //    store person
  412.     POET_Error ("     Object is copied   ", erc);        //    show status 
  413.     
  414.     if (blob)        //    Delete blob, if needed  
  415.         delete blob;
  416. }
  417.  
  418.  
  419. void CEditPersDlg::OnClickedDelete()
  420. {
  421.     int    erc;
  422.     
  423.     if (person->IsStored ())    //    only delete if exist
  424.        {
  425.         erc = person->Delete (PtDEEP);                        //    delete current person
  426.         POET_Error ("     Object is deleted  ", erc);          //    show status
  427.         person->UnAssign ();                //    deleted person is able to be stored again
  428.         person->Assign (theApp.GetOA ());   //    
  429.        }    
  430. }
  431.  
  432.  
  433. void CEditPersDlg::OnClickedPrev()
  434. {
  435.     Person    *newperson;   
  436.     int erc;
  437.  
  438.     if ((erc = allperson->Seek (-1L, PtCURRENT)) == 0)    //    same as next with negative step 1
  439.         erc = allperson->Get (newperson);
  440.     if (erc == 0)
  441.        {
  442.         person->Forget ();
  443.         person = newperson;
  444.         ClearControls ();
  445.         Show ();                                        //    show actual person
  446.        }
  447.     else
  448.         POET_Error ("Previous", erc);                    //    show status
  449. }
  450.  
  451.  
  452. void CEditPersDlg::OnClickedAsspic()
  453. {
  454.     int nResult;
  455.     CString fullpath;
  456.     
  457.     CFileDialog filedlg (TRUE,"*.bmp", "*.bmp", OFN_HIDEREADONLY |    //    initialize dialog
  458.         OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR,
  459.         "Windows Bitmapfile (*.bmp)|*.bmp||",
  460.         this);
  461.     filedlg.m_ofn.lpstrTitle = "Select Image";    //    start dialog
  462.     nResult = filedlg.DoModal ();
  463.     if (nResult == IDOK)
  464.        {
  465.         char    *szFileName;               
  466.         
  467.         fullpath  = filedlg.GetPathName ();
  468.         szFileName = fullpath.GetBuffer (fullpath.GetLength ());    //    get filename and path
  469.         fullpath.ReleaseBuffer ();
  470.  
  471.         Picture    pic;    //    Create a new Picture object
  472.             
  473.         LoadBitmap ( szFileName, &pic.picture );     //    Load Bitmap from File
  474.         
  475.         int err = 0;
  476.  
  477.         if ( (err = pic.Assign(theApp.GetOA())) != 0)     //    Assign the Picture object to the data base
  478.             POET_Error ("Assign", err);
  479.         else
  480.            {    //    Delete the old picture from the database, if available
  481.             if ( person->pict.IsReferenced () )
  482.                 person->pict.Delete ();
  483.         
  484.             person->pict.SetReference(&pic);    //    Set the reference of the Picture in the
  485.                                                 //    Person object to the new object
  486.             Fill ();                            //  Update person 
  487.             if ((err = person->Store( PtDEEP )) != 0)    //    Store the Person, the reference and the picture
  488.                 POET_Error ("Store Person", err);
  489.                else
  490.                    MessageBox("Picture Assigned and Stored", "Hello Message", MB_OK);
  491.            }
  492.        }    
  493.     
  494.     filedlg.EndDialog (nResult);    //    end dialog
  495. }
  496.  
  497.  
  498.  
  499. void CEditPersDlg::OnChangeEditFirstname()
  500. {
  501.     firstname_is_modified = 1;      //    set modify-flag to TRUE
  502. }
  503.  
  504.  
  505. void CEditPersDlg::OnKillfocusEditFirstname()
  506.     if (firstname_is_modified != 0)
  507.        {
  508.         UpdateData ();        //    recieve data from dialog
  509.         if ((item = m_CListFirstnames.GetCurSel ()) >= 0)    //    firstname selected in the list ? 
  510.            {
  511.                m_CListFirstnames.DeleteString (item);            //    delete firstname in list
  512.             if (m_strFirstname.GetLength () != 0)        //    firstname in edit not empty ?
  513.                 m_CListFirstnames.InsertString (item, m_strFirstname);    //    insert firstname in list
  514.            }
  515.         else
  516.               m_CListFirstnames.AddString (m_strFirstname);  //    puts new element in list
  517.         firstname_is_modified = 0;  //    set modify-flag to FALSE      
  518.         UpdateData (FALSE);            //    send data to dialog
  519.        }
  520. }
  521.  
  522. void CEditPersDlg::OnDblclkListFirstnames()
  523. {
  524.     UpdateData ();    //    recieve data from dialog
  525.     m_CListFirstnames.GetText (m_CListFirstnames.GetCurSel (), m_strFirstname);    //    set doubleclicked firstname
  526.     UpdateData (FALSE);    //    send data to dialog
  527.     PrevDlgCtrl ();        //    set focus to firstname-editcontrol
  528. }
  529.  
  530.  
  531. void CEditPersDlg::OnClickedClear()
  532. {                                  
  533.     person->UnAssign ();    //  cut person's connection to database
  534.     person->Forget ();        //    actual type of person is not safe,  
  535.     NewPersonPtr ();    //    set correct type of person    
  536.     person->Init ();            //    initialize person's attributes 
  537.     person->Assign (theApp.GetOA ());    //    assign it to database
  538.     Show ();                            //    show actual person
  539. }
  540.  
  541.  
  542. void CEditPersDlg::OnClickedShowpic()
  543. {
  544.     Picture *picture = NULL;    
  545.  
  546.     int err = person->pict.Get(picture);    //    get Blob
  547.  
  548.     if (!err && picture && picture->IsAssigned())
  549.     {   
  550.         int    nResult;
  551.  
  552.         CShowPictDlg    showpict (this, picture->GetPicture ());
  553.         nResult = showpict.DoModal ();        //    start dialog
  554.         showpict.EndDialog (nResult);
  555.         person->pict.Unget (picture);        //    free memory allocated by Get
  556.     }
  557.     else
  558.         MessageBox("No Picture Assigned", "Hello Alert", MB_OK);
  559. }
  560.  
  561.  
  562.  
  563.